package com.olymtech.jbricks.demo.freemarker;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author haoting.wang
 * @date 2020/12/17 5:34 下午
 **/
public class VelocityDemo {

    private static final String VM_PATH = "velocity/demo.vm";


    public static void main(String[] args) {
        // 初始化模板引擎
        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityEngine.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityEngine.init();

        // 获取模板文件
        Template template = velocityEngine.getTemplate(VM_PATH);

        // 设置变量，velocityContext是一个类似map的结构
        VelocityContext velocityContext = new VelocityContext();

        velocityContext.put("hello", "王浩艇");
        velocityContext.put("委托人简称", "王浩艇");
        velocityContext.put("提单号", "TD12413252436262");
        velocityContext.put("工作编号", "GZBH21351515");
        velocityContext.put("客户编号", "KHBH23452362");
        velocityContext.put("预计开船时间", "2020-12-12");
        velocityContext.put("船名/航次", "1818黄金眼");

        // 输出渲染后的结果
        StringWriter stringWriter = new StringWriter();
        template.merge(velocityContext, stringWriter);
        System.out.println(stringWriter.toString());
    }

}
